#include "LanguageModel.h"

LanguageModel::LanguageModel( QObject * parent ) :
    QAbstractTableModel( parent ),
    languageElementTable(),
    currentIndex( 0 ),
    modified( false )
{
}

QVariant LanguageModel::data( const QModelIndex & index, int role ) const
{
    if( !index.isValid() )
    {
        return QVariant();
    }

    if( index.row() >= 0 && index.row() < languageElementTable.size() )
    {
        switch( index.column() )
        {
            case 0 :
            {
                if( role == Qt::DisplayRole || role == Qt::EditRole )
                {
                    return languageElementTable[ index.row() ].text;
                }
            }
            break;
            
            case 1 :
            {
                if( role == Qt::DisplayRole || role == Qt::EditRole )
                {
                    return languageElementTable[ index.row() ].status;
                }
            }
            break;
            
            case 2 :
            {
                if( role == Qt::DisplayRole || role == Qt::EditRole )
                {
                    return languageElementTable[ index.row() ].index;
                }
            }
            break;
        }
    }
    
    return QVariant();
}

bool LanguageModel::setData(const QModelIndex &index, const QVariant &value, int role )
{
    if( !index.isValid() )
    {
        return false;
    }
    if( index.row() >= 0 && index.row() < languageElementTable.size() )
    {
        switch( index.column() )
        {
            case 0 :
            {
                if( role == Qt::EditRole && languageElementTable[ index.row() ].text != value.toString() )
                {
                    languageElementTable[ index.row() ].text = value.toString();
                    emit dataChanged( index, index );
                    setModified();
                    return true;
                }

                break;
            }
            case 1 :
            {
                if( role == Qt::EditRole && languageElementTable[ index.row() ].status != value )
                {
                    languageElementTable[ index.row() ].status = value.toInt();
                    emit dataChanged( index, index );
                    setModified();
                    return true;
                }

                break;
            }
            case 2 :
            {
                if( role == Qt::EditRole && languageElementTable[ index.row() ].index != value )
                {
                    languageElementTable[ index.row() ].index = value.toInt();
                    emit dataChanged( index, index );
                    setModified();
                    return true;
                }

                break;
            }
        }
    }
    
    return false;
}


void LanguageModel::setModified()
{
    if( !modified )
    {
        modified = true;
        emit contentsChanged();
    }
}

int LanguageModel::getRowIndex( int symbol_index )
{
    int i;
    int count = languageElementTable.count();

    for( i = 0; i < count; i++ )
    {
        if ( languageElementTable[ i ].index == symbol_index )
        {
            return i;
        }
    }

    beginInsertRows( QModelIndex(), count, count );
    {
        LanguageElement languageElement;

        languageElement.index = symbol_index;
        languageElement.status = 0;

        languageElementTable.append( languageElement );
    }
    endInsertRows();

    return i;
}

bool LanguageModel::rowIsComplete( int symbol_index )
{
    int i;
    int count = languageElementTable.count();

    for( i = 0; i < count; i++ )
    {
        if ( languageElementTable[ i ].index == symbol_index )
        {
            return !languageElementTable[ i ].text.isEmpty();
        }
    }
    
    return false;
}

void LanguageModel::initializeModel( const QVector<LanguageElement> & element_table )
{
    beginResetModel();
    languageElementTable = element_table;
    endResetModel();
}